package ua.maxicash.service;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ua.maxicash.vo.SystemUser;

import java.util.ArrayList;
import java.util.Collection;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	private UserSecurityService userSecurityService;
	
	@Autowired
	private Assembler assembler;

	@Transactional(readOnly = true)
	  public UserDetails loadUserByUsername(String username)
	      throws UsernameNotFoundException, DataAccessException {

	    UserDetails userDetails = null;
	    SystemUser su = userSecurityService.getSystemUserByCredentials(username);
	    if (su == null)
	      throw new UsernameNotFoundException("user not found");

	    return assembler.buildUserFromUserEntity(su);
	  }

}